<?php

/**
 * @description Holp You Do Good But Not Evil
 * @copyright   Copyright 2014-2015 <ycchen@iwshop.cn>
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chenyong Cai <ycchen@iwshop.cn>
 * @package     Wshop
 * @link        http://www.iwshop.cn
 */
class wProduct extends Controller {

    /**
     * 商品上下架
     */
    public function switchOnline() {
        $productId = $this->pPost('productId');
        $isOnline = $this->pPost('isOnline');
        echo $this->Dao->update(TABLE_PRODUCTS)->set(array('product_online' => $isOnline))->where('product_id=' . $productId)->exec();
    }

    /**
     * ajax获取商品分类列表树
     * @param type $Query
     */
    public function ajaxGetCategroys() {
        $this->loadModel('SqlCached');
        // file cached
        $cacheKey = 'ajaxGetCategroys';
        $fileCache = new SqlCached();
        $ret = $fileCache->get($cacheKey);
        if (-1 === $ret) {
            $this->loadModel('Product');
            $cats = $this->toJson($this->Product->getAllCats());
            $fileCache->set($cacheKey, $cats);
            echo $cats;
        } else {
            echo $ret;
        }
    }

    /**
     * ajax编辑分类信息
     */
    public function ajaxAlterCategroy() {
        $id = intval($this->post('id'));
        if ($id > 0) {
            $this->loadModel('Product');
            $catinfo = $this->Product->getCatInfo($id);
            if ($id > 0) {
                $set = array();
                $data = $this->post('data');
                foreach ($data as &$d) {
                    if ($d['name'] == 'cat_image' && is_file(dirname(__FILE__) . '/../../uploads/banner_tmp/' . $d['value'])) {
                        if (is_file(dirname(__FILE__) . '/../../' . $catinfo['cat_image'])) {
                            @unlink(dirname(__FILE__) . '/../../' . $catinfo['cat_image']);
                        }
                        @rename(dirname(__FILE__) . '/../../uploads/banner_tmp/' . $d['value'], dirname(__FILE__) . '/../../uploads/banner/' . $d['value']);
                        $d['value'] = 'uploads/banner/' . $d['value'];
                    }
                    $set[] = "`$d[name]` = '$d[value]'";
                }
                $set = implode(',', $set);
                $sql = "UPDATE `product_category` SET $set WHERE `cat_id` = $id";
                echo $this->Db->query($sql);
            }
        }
    }

    /**
     * 
     * @param type $catname
     * @param type $pid
     */
    public function ajaxDelCategroy() {
        $id = $this->post('id');
        $this->loadModel('Product');
        echo $this->delCategroy($id);
    }

    /**
     * 递归删除分类
     * @param type $id
     * @return boolean
     */
    private function delCategroy($id) {
        $catinfo = $this->Product->getCatInfo($id);
        if ($catinfo['cat_id'] > 0) {
            if (is_file(dirname(__FILE__) . '/../../' . $catinfo['cat_image'])) {
                @unlink(dirname(__FILE__) . '/../../' . $catinfo['cat_image']);
            }
            // 下移分类商品
            $this->Dao->update(TABLE_PRODUCTS)->set(array('product_cat' => 0))->where("product_cat = $id")->exec();
            // 下级分类
            $subcat = $this->Dao->select('cat_id')->from(TABLE_PRODUCT_CATEGORY)->where("cat_parent = $id")->exec();
            // 删除分类
            $this->Db->query("DELETE FROM `product_category` WHERE cat_id = $id;");
            foreach ($subcat as $cat) {
                $this->delCategroy($cat['cat_id']);
            }
            return true;
        } else {
            return false;
        }
    }

}
